setwd(" .... /ChinaTechCoercion_ISQ_ReplicationData") #Set working directory

install.packages("dplyr")
install.packages("tidyr")
install.packages("ggplot2")
install.packages("lubridate")
install.packages("ggformula")
install.packages("knitr")

library(dplyr)
library(tidyr)
library(ggplot2)
library(lubridate)
library(ggformula)
library(knitr)
theme_set(theme_minimal())

################################## FIGURES IN MAIN MANUSCRIPT ############################

#### Figure 1
# Load data
theme_data <- readr::read_csv("ThemeData.csv") 

# Plot Figure 1 (Click on Zoom in output pane to see full figure))
theme_data %>%
  filter(theme == "Technology security" | theme=="De-coupling" ) %>%
  ggplot(aes(y_m, prop_theme_ftlength, group = theme)) +
  geom_line(aes(linetype=theme, color=theme), size=1.2) +
  scale_color_manual(values=c("black",  "grey55")) +
  scale_linetype_manual(values=c("solid",   "dotdash")) +
  scale_x_date(date_labels = "%Y", breaks = "1 year",
               limits = c(min(theme_data$y_m), max(theme_data$y_m))) +
  labs(x = "Year",
       y = "Word Proportion",
       title = "",
       subtitle = "") +
  geom_vline(xintercept = as.Date("2013-06-01"), linetype = "dotted", color = "#780A0B") +
  annotate("text", x = as.Date("2012-11-01"), y = 0.00075, label = "Period of \n Snowden \n revelations \n (in grey) \u2192"  ) +
  geom_vline(xintercept = as.Date("2018-04-01"), linetype = "dotted", color = "#780A0B", size=1) +
  annotate("text", label = "US enacts denial \norder against ZTE  \u2192" ,
           x = as.Date("2017-04-01"), y = 0.00075) +
  annotate("rect", xmin = as.Date("2013-06-01"), xmax = as.Date("2014-05-01"), ymin = -Inf, ymax = Inf, alpha = .2) +
  theme(legend.position="top", legend.text=element_text(size=15), axis.title = element_text(size=15), axis.text = element_text(size=15)) 

###### Figure 2
# Load data
term.indv <- readr::read_csv("TermWordProportions.csv") 
### Function to plot single term: `plot_a_term() `

plot_a_term <- function(x) {
  df <- filter(term.indv, term == x)
  p <- ggplot(df, aes(y_m, prop_term_ftlength, group = term)) +
    geom_line(size=0.9) +
    scale_x_date(date_labels = "%Y", breaks = "1 year") +
    # facet_wrap(~ coastal) +
    theme_minimal() + 
    labs(x = "Year", 
         y = "Word Proportion", 
         title = "Word Proportion",
         subtitle = paste("Term: ", x)) +
    theme(legend.text=element_text(size=15), axis.text = element_text(size=12), axis.title = element_text(size=15)) 
  return(p)
}
# Plot Figure 2
plot_a_term("whole.nation.system") 

# Plot Figure 3
theme_data %>%
  filter(theme == "De-coupling" | theme=="Liberalization" ) %>%
  ggplot(aes(y_m, prop_theme_ftlength, group = theme)) +
  geom_line(aes(linetype=theme, color=theme), size=1) +
  scale_color_manual(values=c("black",  "black")) +
  scale_linetype_manual(values=c("solid",   "dashed")) +
  scale_x_date(date_labels = "%Y", breaks = "1 year",
               limits = c(min(theme_data$y_m), max(theme_data$y_m))) +
  labs(x = "Year",
       y = "Word Proportion",
       title = "",
       subtitle = "") +
  annotate("rect", xmin = as.Date("2018-04-01"), xmax = as.Date("2021-12-01"), ymin = -Inf, ymax = Inf, alpha = .2) +
  theme(legend.position="top", legend.text=element_text(size=12), axis.title = element_text(size=12), axis.text = element_text(size=12)) 


B <- read.csv("AnnualCount_ByType_CentralDocs_0521.csv")

# Plot Figure 4
ggplot(B[B$Authority=="Party, Legislative, Executive" & B$Keyword=="Cybersecurity",],
            aes(Year, Number, fill=Keyword)) +
  geom_bar(stat = "identity", position = 'dodge') +
  theme(axis.text.x = element_text(angle = 90)) +
  scale_x_continuous(breaks=seq(2005, 2021, 1)) +
  scale_fill_grey(start = .6, end = .6) +
  theme_bw() +
  theme(legend.position = "none") 

# Plot Figure 5
ggplot(B[B$Authority=="Party, Legislative, Executive" & B$Keyword=="Chokepoint" | 
                B$Authority=="Party, Legislative, Executive" & B$Keyword=="Whole Nation System",],
            aes(Year, Number, fill=Keyword)) +
  geom_bar(stat = "identity", position = 'dodge') +
  theme(axis.text.x = element_text(angle = 90)) +
  scale_x_continuous(breaks=seq(2005, 2021, 1)) +
  scale_y_continuous(breaks=seq(0, 12, 1)) +
  scale_fill_grey(start = .3, end = .8) +
  theme_bw()



################################## FIGURES IN SUPPLEMENTARY APPENDIX ############################


#### Figure A1

F.A1 <- readr::read_csv("FigureA1.csv") 

F.A1 %>%
  ggplot(aes(y_m, count_per_month)) +
  geom_line() +
  guides(linetype = FALSE, shape = FALSE) +
  scale_x_date(date_labels = "%Y", breaks = "1 year", # only display year
               limits = c(min(F.A1$y_m), max(F.A1$y_m))) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  labs(x = "Year",
       y = "",
       title = "Annual Count of S&T-related Articles in Corpus (2005-2021)") +
  theme_minimal()

#### Table A1
T.A1 <- readr::read_csv("TableA1.csv") 
kable(T.A1[,c(2,3,5)], digits=2)

#### Figure A2
F.A2 <- readr::read_csv("FigureA2.csv") 

F.A2 %>%
  ggplot(aes(y_m, share_type_per_month, group = Type)) +
  geom_line(aes(linetype = Type)) +
  geom_dl(aes(label = Type), method = list(dl.trans(x = x - 3.7, y = y  ), "last.points")) +
  guides(linetype = FALSE, shape = FALSE) +
  scale_x_date(date_labels = "%Y", breaks = "1 year",
               limits = c(min(F.A2$y_m), max(F.A2$y_m))) +
  labs(x = "Year",
       y = "Share of articles",
       title = "Breakdown of Articles by Type of Publication (Share of Total)") +
  theme_minimal()

#### Table A2
T.A2 <- readr::read_csv("TableA2.csv") 

kable(T.A2, digits=2)

#### Figure A3
F.A3 <- readr::read_csv("FigureA3.csv") 

F.A3 %>%
  ggplot(aes(y_m, share_focus_per_month, group = Focus5)) +
  geom_line(aes(linetype = as.factor(Focus5))) +
  geom_dl(aes(label = Focus5), method = list(dl.trans(x = x - 3.5, y = y ), cex=.8, "last.points")) +
  guides(linetype = FALSE, shape = FALSE) +
  scale_x_discrete(breaks = seq(2005, 2010, by = 1)) +
  scale_x_date(date_labels = "%Y", breaks = "1 year",
               limits = c(min(F.A3$y_m), max(F.A3$y_m))) +
  theme(axis.text.x  = element_text(size = 10, angle = 45, colour = "black",
                                    vjust = 1, hjust = 1)) +
  labs(x = "Year",
       y = "Share of articles",
       title = "Breakdown of Articles by Substantive Focus of Publication (Share of Total)") +
  theme_minimal()


###### Figure B1

plot_a_term("network.security") 


###### Figure B2
#Load data
theme_data_focus <- readr::read_csv("ThemeDataFocus.csv") 

### Function to Figure B2
plot_a_theme_focus_smooth2 <- function(x) {
  df <- filter(theme_data_focus[theme_data_focus$Focus5=="General" | theme_data_focus$Focus5=="Economy & Industry" |  theme_data_focus$Focus5=="Specialized"
                                | theme_data_focus$Focus5=="S&T"   |theme_data_focus$Focus5=="Security",], theme == x)
  p <- ggplot(df, aes(y_m, prop_theme_ftlength_focus, group = theme)) +
    geom_point(aes(col=theme)) +
    stat_spline() +
    # geom_line() +
    scale_color_manual(values="grey") +
    scale_x_date(date_labels = "%Y", breaks = "1 years") +
    ylim(-0.00025,0.0015) +
    facet_wrap(~ Focus5) +
    # theme_minimal() + 
    labs(x = "Year", 
         y = "Word Proportion", 
         title = "Key word prevalence",
         subtitle = paste("Theme: ", x)) +
    theme(title=element_text(size=15), axis.title = element_text(size=15), axis.text = element_text(size=8),axis.text.x=element_text(angle = -90, hjust = 0),
          legend.position = "none")
  return(p)
}
# Plot Figure B2 (Figures may take a while to load)
plot_a_theme_focus_smooth2("Technology security") 


### Function to plot Figure B3
plot_a_theme_focus_smooth <- function(x) {
  df <- filter(theme_data_focus[theme_data_focus$Focus5=="General" | theme_data_focus$Focus5=="Economy & Industry" |  theme_data_focus$Focus5=="Specialized"
                                | theme_data_focus$Focus5=="S&T"   |theme_data_focus$Focus5=="Security",], theme == x)
  p <- ggplot(df, aes(y_m, prop_theme_ftlength_focus, group = theme)) +
    geom_point(aes(col=theme)) +
    stat_spline() +
    scale_color_manual(values="grey") +
    scale_x_date(date_labels = "%Y", breaks = "1 years") +
    facet_wrap(~ Focus5) +
    labs(x = "Year", 
         y = "Word Proportion", 
         subtitle = paste("Theme: ", x)) +
    theme(title=element_text(size=15), axis.title = element_text(size=15), axis.text = element_text(size=8),axis.text.x=element_text(angle = -90, hjust = 0),
          legend.position = "none")
  return(p)
}
# Plot Figure B3 (Figures may take a while to load)
plot_a_theme_focus_smooth("De-coupling") 

######## Figure B4
#Load data
theme_data_STDaily <- readr::read_csv("ThemeDataFocus_STDaily.csv") 

#Plot Figure B4
theme_data_STDaily %>%
  filter(theme == "De-coupling" | theme=="Liberalization" ) %>%
  ggplot(aes(y_m, prop_theme_ftlength, group = theme)) +
  stat_spline(aes(linetype=theme), size=1) +
  scale_color_manual(values=c("black", "black")) +
  scale_linetype_manual(values=c("solid", "dashed")) +
  scale_x_date(date_labels = "%Y", breaks = "1 years") +
  annotate("rect", xmin = as.Date("2018-04-01"), xmax = as.Date("2021-12-01"), ymin = -Inf, ymax = Inf, alpha = .2) +
  labs(x = "Year", 
       y = "Theme Proportion", 
       title = "Theme prevalence in Science and Technology Daily") +
  theme(title=element_text(size=15), axis.title = element_text(size=15), axis.text = element_text(size=15), legend.position="top") 

### End
